package com.example.tao_city.text1;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Created by TAO_CITY on 2018/1/8.
 */

public class Loginpassword extends AppCompatActivity implements View.OnClickListener {
    private ImageView back;
    private Button confirm;
    private TextView oldpassword, newpassword, passwordagin;
    private RelativeLayout baselayout;

    @Override
    protected void onCreate(Bundle savep) {
        super.onCreate(savep);
        setContentView(R.layout.loginpassword);
        initbutton();
    }

    private void initbutton() {
        back = findViewById(R.id.lpw_back);
        confirm = findViewById(R.id.lpw_confirm);
        oldpassword = findViewById(R.id.lpw_oldpassword);
        newpassword = findViewById(R.id.lpw_newpassword);
        passwordagin = findViewById(R.id.lpw_passwordagin);
        baselayout=findViewById(R.id.loginpassword_baselayout);

        confirm.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lpw_back:
                finish();
                break;
            case R.id.lpw_confirm:
                String op = String.valueOf(oldpassword.getText());
                String np = String.valueOf(newpassword.getText());
                String pa = String.valueOf(passwordagin.getText());
                if (op.equals("123456") && np.equals(pa) && !(np.equals(""))) {
                    MyToast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    screenchange();
                    Runnable ra = new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    };
                    Handler ahander = new Handler();
                    ahander.postDelayed(ra, 1000);
                } else if (!(op.equals("123456"))) {
                    MyToastf.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();
                    screenchange();
                } else if (!(np.equals(pa))) {
                    MyToastf.makeText(this, "新密码不一致", Toast.LENGTH_SHORT).show();
                    screenchange();
                }
                break;
        }

    }

    private void screenchange() {
        Handler mhander = new Handler();//新建一个Hander
        mhander.postDelayed(ma, 2000);//两秒后执行ma线程
        //下面4行代码是屏幕变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.6f;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        back.setClickable(false);
        oldpassword.setEnabled(false);
        newpassword.setEnabled(false);
        passwordagin.setEnabled(false);

    }

    private Runnable ma = new Runnable() {
        @Override
        public void run() {
            //下面4行代码使屏幕变回亮的样式
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 1.0f;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            back.setClickable(true);
            oldpassword.setEnabled(true);
            newpassword.setEnabled(true);
            passwordagin.setEnabled(true);
        }
    };

}

class MyToast {//自定义Toast样式
    private Toast mToast;

    MyToast(Context context, CharSequence text, int duration) {//这个函数的参数可以由自定义的样式也main的需求添加参数，
                                                                            // 可以在函数里面设置这些值，调用时输入所需参数
        View v = LayoutInflater.from(context).inflate(R.layout.mytoast, null);
        TextView textView = v.findViewById(R.id.mytost_text);
        ImageView imageView = v.findViewById(R.id.mytost_img);
        textView.setText(text);//设置显示文字位设置的文字
        mToast = new Toast(context);
        mToast.setDuration(duration);
        mToast.setView(v);//设置显示样式视图
        mToast.setGravity(Gravity.CENTER, 0, -200);//设置位置
    }

    public static MyToast makeText(Context context, CharSequence text, int duration) {
        return new MyToast(context, text, duration);
    }

    public void show() {
        if (mToast != null) {
            mToast.show();
        }
    }
}

class MyToastf {
    private Toast mToastf;

    private MyToastf(Context context, CharSequence text, int duration) {
        View v = LayoutInflater.from(context).inflate(R.layout.mytoastf, null);
        TextView textView = v.findViewById(R.id.mytostf_text);
        ImageView image= v.findViewById(R.id.mytostf_img);
        textView.setText(text);
        mToastf = new Toast(context);
        mToastf.setDuration(duration);
        mToastf.setView(v);
        mToastf.setGravity(Gravity.CENTER, 0, -200);
    }

    public static MyToastf makeText(Context context, CharSequence text, int duration) {
        return new MyToastf(context, text, duration );
    }

    public void show() {
        if (mToastf != null) {
            mToastf.show();
        }
    }
}
